package org.wangchaocheng.simpleadmin.statemachine;

import com.alibaba.cola.statemachine.StateMachine;
import com.alibaba.cola.statemachine.builder.StateMachineBuilder;
import com.alibaba.cola.statemachine.builder.StateMachineBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.wangchaocheng.simpleadmin.statemachine.enums.EventEnum;
import org.wangchaocheng.simpleadmin.statemachine.enums.StateEnum;

@Component
public class TPDStateMachine {

    @Autowired
    private ConditionService conditionService;

    @Autowired
    private ActionService actionService;

    @Bean
    public StateMachine<StateEnum, EventEnum, StateMachineContext> stateMachine() {

        StateMachineBuilder<StateEnum, EventEnum, StateMachineContext> builder = StateMachineBuilderFactory.create();
        // 从INIT状态转化到DOWNLOAD状态，在INIT_CHECK事件下，当满足init2DownloadCondition条件时，执行downloadOriginalPackage内的逻辑
        builder.externalTransition().from(StateEnum.INIT).to(StateEnum.DOWNLOAD)
                .on(EventEnum.INIT_CHECK)
                .when(conditionService.init2DownloadCondition())
                .perform(actionService.downloadOriginalPackage());

        builder.externalTransition().from(StateEnum.DOWNLOAD).to(StateEnum.SIGNATURE)
                .on(EventEnum.START_SIGNATURE)
                .perform(actionService.sign());



        return builder.build("threePartyDeliveryPackageSignStateMachine");
    }


}
